package org.example.process.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.example.model.process.ProcessTemplate;

import org.example.model.process.ProcessType;
import org.example.process.mapper.OaProcessTemplateMapper;
import org.example.process.service.OaProcessTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.process.service.OaProcessTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 审批模板 服务实现类
 * </p>
 *
 * @author example
 * @since 2023-04-19
 */
@Service
public class OaProcessTemplateServiceImpl extends ServiceImpl<OaProcessTemplateMapper, ProcessTemplate> implements OaProcessTemplateService {

    @Autowired
    private OaProcessTypeService processTypeService;



    @Override
    public IPage<ProcessTemplate> selectPageProcessTemlate(Page<ProcessTemplate> pageParm) {
        //        1 调用mapper的方法实现分页查询
        Page<ProcessTemplate> processTemplatePage=baseMapper.selectPage(pageParm,null);
//        2 第一步分页查询返回分页数据，从分页数据获取列表list集合
        List<ProcessTemplate> processTemplatesList=processTemplatePage.getRecords();
//        3遍历list集合，得到每个对象审批类型的id
            for (ProcessTemplate processTemplate:processTemplatesList){
                Long processTypeId=processTemplate.getProcessTypeId();
                //        4 根据审批类型id 查询对应名称
                LambdaQueryWrapper<ProcessType> wrapper=new LambdaQueryWrapper<>();
                wrapper.eq(ProcessType::getId,processTypeId);
                ProcessType processType =processTypeService.getOne(wrapper);
                if (processType==null){
                    continue;
                }
                processTemplate.setProcessTypeName(processType.getName());
            }
//        5完成最终的封装
        return processTemplatePage;
    }

    //    部署流程定义
    @Override
    public void publish(Long id) {
     ProcessTemplate processTemplate=baseMapper.selectById(id);
     processTemplate.setStatus(1);
     baseMapper.updateById(processTemplate);

//     todd后续完善，流程定义部署
    }
}
